package com.okb.service.impl.sale;

import com.alibaba.fastjson.JSON;
import com.okb.bean.OrderResponseBean;
import com.okb.bean.ResponseBean;
import com.okb.dto.CancelOderDto;
import com.okb.dto.OrderRequestDto;
import com.okb.service.SaleOrderService;
import com.okb.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * Author: hcxu
 * Time: 2024/11/22 1:44 PM
 */
@Service
@Slf4j
public class SaleOrderServiceImpl implements SaleOrderService {
    private static final String SALE_URL = "/api/v5/trade/order";

    private static final String CANCEL_ORDER_URL = "/api/v5/trade/cancel-order";

    @Override
    public String saleStockOrder(String stockType, Long stockNum, String algoId,String price,String side) {
        OrderRequestDto orderRequestDto = new OrderRequestDto();
        orderRequestDto.setInstId(stockType);
        orderRequestDto.setTdMode("cash");
        orderRequestDto.setClOrdId(algoId);
        orderRequestDto.setSide(side);
        orderRequestDto.setOrdType("limit");
        orderRequestDto.setPx(price);
        orderRequestDto.setSz(stockNum.toString());
        String jsonBody = JSON.toJSONString(orderRequestDto);
        try {
            Map<String, String> header = HttpUtils.getHeader(SALE_URL, HttpUtils.REQUEST_TYPE_POST, jsonBody);
            ResponseBean<OrderResponseBean> responseBean = HttpUtils.postRequestJson(HttpUtils.method_pre + SALE_URL, jsonBody, header, OrderResponseBean.class);
            log.info("请求订单信息:{}",responseBean);
            if(responseBean.getCode()){
                List<OrderResponseBean> data = responseBean.getData();
                return data.get(0).getOrdId();
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean cancelStockOrder(String stockType,String orderId,String algoId){
        CancelOderDto cancelOderDto = new CancelOderDto();
        cancelOderDto.setInstId(stockType);
        cancelOderDto.setOrdId(orderId);
        cancelOderDto.setClOrdId(algoId);
        String jsonBody = JSON.toJSONString(cancelOderDto);
        try {
            Map<String, String> header = HttpUtils.getHeader(CANCEL_ORDER_URL, HttpUtils.REQUEST_TYPE_POST, jsonBody);
            ResponseBean<OrderResponseBean> responseBean = HttpUtils.postRequestJson(HttpUtils.method_pre + CANCEL_ORDER_URL, jsonBody, header, OrderResponseBean.class);
            log.info("撤单信息:{}",responseBean);
            return responseBean.getCode();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //2005034132859617280
    public static void main(String[] args) {
        SaleOrderService service = new SaleOrderServiceImpl();
        String orderId = service.saleStockOrder("BTC-USDT", 1l, "Xu9798","99180","buy");
        if(StringUtils.isNotBlank(orderId)){
            //System.out.println(service.cancelStockOrder("BTC-USDT", orderId, ""));
        }
    }
}
